<template>
    <div class="edit-popup">
        <el-card class="!border-none" shadow="never">
            <el-page-header content="发文编辑" @back="$router.back()" />
        </el-card>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="客户名称" prop="customerName">
                    <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
                </el-form-item>
                <el-form-item label="发文模板" prop="documentTemplate">
                    <el-select
                        class="flex-1"
                        v-model="formData.documentTemplate"
                        placeholder="请选择发文模板"
                    >
                        <el-option label="年费通知" value="1" />
                        <el-option label="新申请递交" value="2" />
                        <el-option label="驳回决定" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="发文类型" prop="documentType">
                    <el-select
                        class="flex-1"
                        v-model="formData.documentType"
                        placeholder="请选择发文类型"
                    >
                        <el-option label="其他" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="邮件标签" prop="mailLabel">
                    <el-select
                        class="flex-1"
                        v-model="formData.mailLabel"
                        placeholder="请选择邮件标签"
                    >
                        <el-option label="1" value="1" />
                        <el-option label="2" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="完成事项" prop="achieveMatter ">
                    <el-select
                        class="flex-1"
                        v-model="formData.achieveMatter"
                        placeholder="请选择完成事项"
                    >
                        <el-option label="开卷" value="1" />
                        <el-option label="翻译" value="2" />
                        <el-option label="检索" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="附件" prop="accessory">
                    <el-input v-model="formData.accessory" placeholder="请输入附件" />
                </el-form-item>
                <el-form-item label="发件人" prop="addresser">
                    <el-select
                        class="flex-1"
                        v-model="formData.addresser"
                        placeholder="请选择发文类型"
                    >
                        <el-option label="流程01<1260041229@qq.com>" value="1" />
                        <el-option label="流程02<1260041229@qq.com>" value="2" />
                        <el-option label="流程03<1260041229@qq.com>" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="收件人" prop="recipients">
                    <el-input v-model="formData.recipients" placeholder="请输入收件人" />
                </el-form-item>
                <el-form-item label="抄送" prop="copy">
                    <el-input v-model="formData.copy" placeholder="请输入抄送" />
                </el-form-item>
                <el-form-item label="密送" prop="blindCarbonCopy">
                    <el-input v-model="formData.blindCarbonCopy" placeholder="请输入密送" />
                </el-form-item>
                <el-form-item label="邮件描述" prop="mailDescribe">
                    <el-input v-model="formData.mailDescribe" placeholder="请输入邮件描述" />
                </el-form-item>
                <el-form-item label="主题" prop="titleTheme">
                    <el-input v-model="formData.titleTheme" placeholder="请输入主题" />
                </el-form-item>
                <el-form-item label="正文" prop="text">
                    <editor v-model="formData.text" height="500px" class="flex-1" mode="simple" />
                </el-form-item>
                <el-form-item label="邮件签名" prop="mailSignature">
                    <el-select
                        class="flex-1"
                        v-model="formData.mailSignature"
                        placeholder="请选择邮件签名"
                    >
                        <el-option label="1" value="1" />
                        <el-option label="2" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="应返稿日期" prop="responsecopy">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.returnableDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="应返稿日期"
                    />
                </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm">保存</el-button>
              </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { documentrecordEdit, documentrecordAdd, documentrecordDetail } from '@/api/documentrecord'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
const route = useRoute()
const router = useRouter()
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
import useMultipleTabs from '@/hooks/useMultipleTabs'
const { removeTab } = useMultipleTabs()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑发文记录' : '新增发文记录'
})

const formData = reactive({
    id: '',
    recipientsId: '',
    recipients: '',
    theme: '',
    documentTypeId: '',
    documentType: '',
    customerName: '',
    documentTime: '',
    documentTemplate:'',
    mailLabel:'',
    achieveMatter:'',
    accessory:'',
    addresser:'',
    copy:'',
    blindCarbonCopy:'',
    mailDescribe:'',
    titleTheme:'',
    text:'',
    mailSignature:'',
    returnableDate:''
})

const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    if (route.query.id) {
        await documentrecordEdit(data)
    } else {
        await documentrecordAdd(data)
    }
    //   mode.value == "edit"
    //     ? await documentrecordEdit(data)
    //     : await documentrecordAdd(data);
    //popupRef.value?.close();
    feedback.msgSuccess('操作成功')
    emit('success')
    removeTab()
    router.back()
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const submitForm = async () => {
  const data = await documentrecordAdd(formData)
}

const getDetail = async () => {
    const data = await documentrecordDetail({
        id: route.query.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
